// pages/smallTeam/smallTeam.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 0,
    teamId: [],//球队id
    teamName: [],//球队名字
    teamLogo: [],//球队logo
    teamIndex: 0,
    session_key: '',
    team_member:[],//队员列表
    groupingJson:{},//分组对象
    groupingArry:[],//分组数据
    groupingName:'',
    checkboxUserid:[],
    httpPath: app.globalData.imagePath
  },
  /**
  * 生命周期函数--监听页面加载
  */
  onLoad: function (options) {
    var that = this;
    that.setData({
      session_key: wx.getStorageSync("token")
    })
    //获取球队信息
    wx.request({
      url: app.globalData.httpsPath + '/user/team',
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      data: {
        token: that.data.session_key
      },
      //请求后台数据成功  
      success: function (res) {
        var teamidArry = [];
        var teamArry = [];
        var teamLogo = [];
        var _index = 0;
        var _createTeam = res.data.data.data;
        for (var i = 0; i < _createTeam.length; i++) {
          teamidArry.push(_createTeam[i].team_id);
          teamArry.push(_createTeam[i].team.name);
          teamLogo.push(_createTeam[i].team.logo);
        }
        that.setData({
          teamId: teamidArry,
          teamName: teamArry,
          teamLogo: teamLogo
        })
      }
    })
    //获取小分队列表
    that.getGroupinglist();
  },
  //点击切换
  clickTab: function (e) {
    var that = this;
    if (that.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current,
      });
      if (e.target.dataset.current == "0") {//我的小分队

        //获取小分队列表
        //that.getGroupinglist();

      } else {//创建小分队
        var _teamlist = that.data.teamId;
        var _teamid;
        if (_teamlist.length != 0){
          _teamid = _teamlist[that.data.teamIndex]
        }
        wx.showLoading({
          title: '数据加载中'
        })
        //球队队员
        wx.request({
          url: app.globalData.httpsPath + '/team/users',
          method: 'POST',
          header: {
            'content-type': 'application/json'
          },
          data: {
            token: that.data.session_key,
            team_id: _teamid
          },
          //请求后台数据成功  
          success: function (res) {
            wx.hideLoading();
            var team_member = res.data.data;
            that.setData({
              team_member: team_member
            })
          }
        })

      }
    }
  },
  //获取小分队列表
  getGroupinglist: function () {
    var that = this;
    wx.showLoading({
      title: '数据加载中'
    })
    wx.request({
      url: app.globalData.httpsPath + '/subteam/list',
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      data: {
        token: that.data.session_key
      },
      //请求后台数据成功  
      success: function (res) {
        wx.hideLoading();
        var fendui_json = res.data.data;
        var fendui_arr = res.data.data.data;
        that.setData({
          groupingJson: fendui_json,
          groupingArry: fendui_arr
        })
      }
    })
  },
  //切换球队信息
  bindTeamChange: function (e) {
    var that = this;
    that.setData({
      teamIndex: e.detail.value
    })
    var teamId = that.data.teamId[e.detail.value];
    var _key = that.data.session_key;
    wx.showLoading({
      title: '数据加载中'
    })
    //球队队员
    wx.request({
      url: app.globalData.httpsPath + '/team/users',
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      data: {
        token: _key,
        team_id: teamId
      },
      //请求后台数据成功  
      success: function (res) {
        wx.hideLoading();
        var team_member = res.data.data;
        that.setData({
          team_member: team_member
        })
      }
    })
  },
  //复选框的事件
  checkboxChange: function (e) {
    // console.log('checkbox发生change事件，携带value值为：', e.detail.value)
    this.setData({
      checkboxUserid: e.detail.value
    })
  },
  //获取分队名
  groupingInput: function (e) {
    this.setData({
      groupingName: e.detail.value
    })
  },
  //创建分队
  createGrouping:function(){
    var that = this;
    var _key = that.data.session_key;
    var _teamId = that.data.teamId[that.data.teamIndex];
    var _teamLogo = that.data.teamLogo[that.data.teamIndex];
    var _groupName = that.data.groupingName;
    var _checkboxId = that.data.checkboxUserid;
    var _checkboxIdstr;
   
    if (_groupName == '') {
      wx.showToast({
        icon: "none",
        title: '请输小分队名称'
      })
      return false;
    }
    if (_checkboxId.length == 0) {
      wx.showToast({
        icon: "none",
        title: '请选择分队成员'
      })
      return false;
    }else{
      _checkboxIdstr = _checkboxId.join(",");
      console.info(_checkboxIdstr)
    }
   
    wx.request({
      url: app.globalData.httpsPath + '/subteam/save',
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      data: {
        token: _key,
        pid: _teamId,
        name: _groupName,
        user_ids: _checkboxIdstr,
        logo: _teamLogo
      },
      //请求后台数据成功  
      success: function (res) {
        wx.showToast({
          title: res.data.msg,
          icon: 'succes',
          duration: 2000,
          mask: true
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '../smallTeam/smallTeam'
          })
        }, 2000)
      }
    }) 
  },

  //手指触摸动作开始 记录起点X坐标
  touchstart: function (e) {
    //开始触摸时 重置所有删除
    let data = app.touch._touchstart(e, this.data.groupingArry)
    this.setData({
      groupingArry: data
    })
  },

  //滑动事件处理
  touchmove: function (e) {
    let data = app.touch._touchmove(e, this.data.groupingArry)
    this.setData({
      groupingArry: data
    })
  },

  //删除事件
  del: function (e) {
    var _that = this;
    //获取列表中要删除项的下标
    var index = e.currentTarget.dataset.index;
    var list = _that.data.groupingArry;
    var _id = e.currentTarget.dataset.subteamid;

    wx.showModal({
      title: '提示',
      content: '确认要删除此小分队吗？',
      success: function (res) {
        if (res.confirm) {
          // console.log('用户点击确定')
          // 删除小分队
          wx.request({
            url: app.globalData.httpsPath + '/subteam/del',
            method: 'POST',
            header: {
              'content-type': 'application/json'
            },
            data: {
              token:_that.data.session_key,
              subteam_id:_id
            },
            //请求后台数据成功  
            success: function (res) {
              //移除列表中下标为index的项
              list.splice(index, 1);
              //更新列表的状态
              _that.setData({
                groupingArry: list
              });
            }
          })
          
        } else if (res.cancel) {
          // console.log('用户点击取消')
        }
      }
    })
  }

 
})

